package com.jthinker.blog.ext.spring.resolver;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

/**
 * User: Gang Chen
 * Date: 2015/8/11 21:14
 */
public class CusSimpleMappingExceptionResolver extends SimpleMappingExceptionResolver
{
    private static final Logger LOGGER = LoggerFactory.getLogger(CusSimpleMappingExceptionResolver.class);

    private String exceptionAttribute = DEFAULT_EXCEPTION_ATTRIBUTE;

    @Override
    public void setExceptionAttribute(String exceptionAttribute)
    {
        this.exceptionAttribute = exceptionAttribute;
    }

    @Override
    protected ModelAndView getModelAndView(String viewName, Exception ex)
    {
        ModelAndView mv = new ModelAndView(viewName);
        if (this.exceptionAttribute != null)
        {
            LOGGER.error("view exception ", ex);
            mv.addObject(this.exceptionAttribute, ex);
        }
        return mv;
    }
}
